package com.example.iot.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.iot.dao.RuleRelationDao;
import com.example.iot.entity.id.RuleNodeId;
import com.example.iot.entity.po.RelationPO;
import com.example.iot.mapper.mysql.RelationMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ChenXiangpeng
 */
@Service
public class RuleRelationDaoImpl extends ServiceImpl<RelationMapper, RelationPO> implements RuleRelationDao {
    @Override
    public List<RelationPO> getRuleNodeRelations(RuleNodeId ruleNodeId) {
        QueryWrapper<RelationPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RelationPO.FROM_ID, ruleNodeId.getIdStr());
        queryWrapper.eq(RelationPO.RELATION_TYPE, "RULE_NODE");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteRelations(RuleNodeId ruleNodeId) {
        QueryWrapper<RelationPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(i -> i.eq(RelationPO.TO_ID, ruleNodeId.getIdStr()).or().eq(RelationPO.FROM_ID, ruleNodeId.getIdStr()));
        queryWrapper.eq(RelationPO.RELATION_TYPE, "RULE_NODE");
        this.baseMapper.delete(queryWrapper);
    }
}
